//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LitwareHR.PrivateApp.Presenters.ConfigCusto;
using System.Collections.Generic;
using Shp.Runtime.Contract;

public partial class ConfigCusto_EditEntity : BasePage, IEditEntityView
{
    EditEntityPresenter _presenter;
    EditEntityPresenter Presenter
    {
        get
        {
            return _presenter;
        }
    }

    private List<EntityField> Fields
    {
        get
        {
            List<EntityField> defFields = ViewState[Constants.FieldsViewStateKey] as List<EntityField>;
            if (defFields == null)
            {
                defFields = new List<EntityField>();
                ViewState[Constants.FieldsViewStateKey] = defFields;
            }
            return defFields;
        }
        set
        {
            ViewState[Constants.FieldsViewStateKey] = value;
        }
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        _presenter = new EditEntityPresenter(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string defName = Request.QueryString[Constants.EntityDefNameQS];
            if (string.IsNullOrEmpty(defName))
                throw new ArgumentNullException(Constants.EntityDefNameQS);

            // Set up a cookie to pick it up for display
            Response.Cookies[Constants.EntityDefNameCookie].Value = defName;
            Response.Cookies[Constants.EntityDefNameCookie].Expires = DateTime.Now.AddMonths(6);
            Presenter.OnViewLoaded(Helpers.TenantAlias, defName);
        }
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        EntityField field = new EntityField();
        field.Name = NameTextBox.Text;
        field.TypeId = new Guid(FieldDropDown.SelectedValue);
        field.TypeName = FieldDropDown.SelectedItem.Text;
        field.IsShared = false;
        Presenter.OnAdd(field, this.Fields.ToArray());
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        Guid id = new Guid("750438de-31a2-4d87-b0c6-28563840947c");
        Presenter.OnSave(id, this.Fields.ToArray());
    }

    protected void CustomFieldsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string key = CustomFieldsGrid.Rows[e.RowIndex].Cells[0].Text;
        Presenter.OnRemove(key, this.Fields.ToArray());
    }

    public void ShowFields(EntityField[] defFields)
    {
        this.Fields = new List<EntityField>(defFields);
        List<EntityField> shared = new List<EntityField>();
        List<EntityField> custom = new List<EntityField>();
        foreach (EntityField field in defFields)
        {
            if (field.IsShared)
            {
                shared.Add(field);
            }
            else
            {
                custom.Add(field);
            }
        }
        SharedFieldsGrid.DataSource = shared;
        SharedFieldsGrid.DataBind();
        CustomFieldsGrid.DataSource = custom;
        CustomFieldsGrid.DataBind();
    }

    public void ShowTypes(GuidStringPair[] types)
    {
        FieldDropDown.DataSource = types;
        FieldDropDown.DataBind();
    }

}